/*
 * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the
 *       distribution.
 *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
 *       its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <string.h>

#include "cmd_util.h"
#include "xz/xz.h"
#include "cmd_xz.h"


#define XZ_DEC_FROM_FLASH

#ifdef XZ_DEC_FROM_FLASH
#include "driver/chip/hal_flash.h"

void print_hex_dump_bytes(const void *addr, size_t len);

#define MFLASH 0

#define XZ_DEC_FILE_ENTER(flash, status) {    \
	if (HAL_Flash_Open(MFLASH, 5000) != HAL_OK) {    \
		CMD_ERR("flash driver open failed\n");    \
		status = -1;    \
	}    \
}
#define XZ_DEC_FILE_EXIT() {    \
	if (HAL_Flash_Close(MFLASH) != HAL_OK) {    \
		CMD_ERR("flash driver close failed\n");    \
	}    \
}
#define XZ_DEC_FILE_READ(addr, data, size, status) {    \
	if (HAL_Flash_Read(MFLASH, addr, data, size) != HAL_OK) {    \
		CMD_ERR("flash read failed\n");    \
		status = -1;    \
	}    \
}
#define XZ_DEC_FILE_WRITE(addr, data, size, status) {    \
	print_hex_dump_bytes(data, size);    \
}

#else
// #include "fs/fatfs/ff.h"
#include "common/framework/fs_ctrl.h"

FIL fp_in, fp_out;

#define XZ_DEC_FILE_ENTER(path, status) {    \
	if (fs_ctrl_mount(FS_MNT_DEV_TYPE_SDCARD, 0) != 0) {    \
		CMD_ERR("mount fail\n");    \
		status = -1;    \
	} else {    \
		if (f_open(&fp_in, path, FA_OPEN_EXISTING | FA_READ) != FR_OK) {    \
			CMD_ERR("open file fail\n");    \
			status = -1;    \
		}    \
		char *temp = path + (strlen(path) - 3);    \
		if (strncmp(temp, ".xz", 3) != 0)    \
			CMD_ERR("open file fail\n");    \
		*temp = '\0';    \
		f_unlink(path);    \
		if (f_open(&fp_out, path, FA_OPEN_ALWAYS | FA_WRITE) != FR_OK) {\
			CMD_ERR("create file fail\n");    \
			status = -1;    \
		}    \
	}    \
}
#define XZ_DEC_FILE_EXIT() {    \
	f_close(&fp_in);    \
	f_close(&fp_out);    \
	fs_ctrl_unmount(FS_MNT_DEV_TYPE_SDCARD, 0);    \
}
#define XZ_DEC_FILE_READ(buff, btr, br, status) {    \
	if (f_read(&fp_in, buff, btr, &br) != FR_OK) {    \
		CMD_ERR("read failed.\n");    \
		status = -1;    \
	}    \
}
#define XZ_DEC_FILE_WRITE(buff, btw, bw) {    \
	f_write(&fp_out, buff, btw, &bw);    \
}

#endif

#define XZ_DEC_INBUF_SIZE     (4 * 1024)
#define XZ_DEC_OUTBUF_SIZE    (16 * 1024)
#define XZ_DEC_DICT_MAX       (32 * 1024)
#define XZ_RATE_COUNT         (1024)

uint8_t xz_array[] = {
	0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00, 0x00, 0x00, 0xFF, 0x12, 0xD9, 0x41, 0x02, 0x01, 0x08, 0x00, 0x21, 0x01, 0x02, 0x00, 0x23, 0x91, 0x59, 0x46, 0xE0,
	0x06, 0x83, 0x03, 0x71, 0x39, 0x00, 0x17, 0x8A, 0x7D, 0xAD, 0xAB, 0x52, 0xA1, 0x44, 0xA3, 0x72, 0xA4, 0x56, 0x7C, 0xF1, 0x52, 0xE9, 0x37, 0xBC, 0xC4,
	0x60, 0xF6, 0x45, 0xF3, 0x75, 0x50, 0x4E, 0x14, 0x27, 0x9E, 0x8F, 0x10, 0xF9, 0x52, 0x2D, 0x31, 0x09, 0xD9, 0x7F, 0xE2, 0x0C, 0xEC, 0xC6, 0x17, 0x60,
	0xB7, 0x80, 0x64, 0xB5, 0x4A, 0xB8, 0xFE, 0x0D, 0x3A, 0x10, 0x8D, 0xD3, 0xF2, 0xC3, 0x47, 0x15, 0x74, 0x88, 0x07, 0x9F, 0xD4, 0x31, 0x2F, 0x97, 0x0D,
	0xB8, 0xE2, 0xF9, 0xB7, 0x1A, 0x49, 0xE0, 0x7D, 0x4D, 0x5B, 0xD6, 0x9D, 0x7E, 0xF3, 0x93, 0x82, 0x86, 0xF1, 0xF9, 0x6C, 0x59, 0x92, 0x38, 0xAE, 0xD0,
	0xA3, 0x6E, 0x57, 0x73, 0x28, 0xCC, 0x19, 0x8F, 0x0E, 0xF9, 0x01, 0x59, 0xBA, 0x60, 0x74, 0x8E, 0x1B, 0xD3, 0x8A, 0x06, 0xA1, 0x9F, 0x6B, 0x76, 0xE5,
	0x3E, 0xC4, 0x37, 0xC0, 0xB2, 0x7A, 0xDD, 0x50, 0x04, 0xB3, 0x31, 0xDD, 0x82, 0xB3, 0x2E, 0x71, 0xBC, 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 0xD6, 0xA3, 0xC8, 0x68, 0xEC, 0xC1, 0xF1,
	0x40, 0xDA, 0x76, 0x43, 0x62, 0x60, 0x28, 0xC4, 0x6C, 0xF4, 0x24, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x85, 0x07, 0x84, 0x0D, 0x00, 0x00, 0x99,
	0x3F, 0xEB, 0x55, 0xA8, 0x00, 0x0A, 0xFC, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x5A,
};

static enum cmd_status cmd_xz_dec_file_exec(char *cmd)
{
	int status = 0;

#ifdef XZ_DEC_FROM_FLASH
	uint32_t addr, len;
	int cnt = cmd_sscanf(cmd, "addr=0x%x len=%d", &addr, &len);
	if (cnt != 2) {
		CMD_ERR("cmd_sscanf return: cnt = %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}
#else
	uint32_t bw;
	char path[256];
	int cnt = cmd_sscanf(cmd, "path=%s", path);
	if (cnt != 1) {
		CMD_ERR("cmd_sscanf return: cnt = %d\n", cnt);
		return CMD_STATUS_INVALID_ARG;
	}
#endif

#ifdef XZ_DEC_FROM_FLASH
	XZ_DEC_FILE_ENTER(0, status);
#else
	XZ_DEC_FILE_ENTER(path, status);
#endif
	if (status != 0)
		return CMD_STATUS_FAIL;

	uint32_t total_len;
#ifdef XZ_DEC_FROM_FLASH
	total_len = len;
#else
	total_len = f_size(&fp_in);
#endif

	struct xz_dec *s;
	struct xz_buf b;
	enum xz_ret xzret;

	uint32_t read_size = 0;
	uint32_t out_len = 0;
	uint8_t *in_buf = malloc(XZ_DEC_INBUF_SIZE);
	if (in_buf == NULL) {
		CMD_ERR("no mem\n");
		return CMD_STATUS_FAIL;
	}

	uint8_t *out_buf = malloc(XZ_DEC_OUTBUF_SIZE);
	if (out_buf == NULL) {
		CMD_ERR("no mem\n");
		free(in_buf);
		return CMD_STATUS_FAIL;
	}
	memset(out_buf, 0, XZ_DEC_OUTBUF_SIZE);

	s = xz_dec_init(XZ_DYNALLOC, XZ_DEC_DICT_MAX);
	if (!s) {
		CMD_ERR("no mem\n");
		free(in_buf);
		free(out_buf);
		return CMD_STATUS_FAIL;
	}

	b.in = in_buf;
	b.in_pos = 0;
	b.in_size = 0;
	b.out = out_buf;
	b.out_pos = 0;
	b.out_size = XZ_DEC_OUTBUF_SIZE;

	CMD_DBG("dec start..\n");
	while (1) {
		if (b.in_pos == b.in_size) {
#ifdef XZ_DEC_FROM_FLASH
			read_size = len > XZ_DEC_INBUF_SIZE ? XZ_DEC_INBUF_SIZE : len;
			XZ_DEC_FILE_READ(addr, in_buf, read_size, status);
			len -= read_size;
			addr += read_size;
#else
			XZ_DEC_FILE_READ(in_buf, XZ_DEC_INBUF_SIZE, read_size, status);
#endif
			if (status != 0) {
				CMD_ERR("read failed.\n");
				break;
			}
			b.in_size = read_size;
			b.in_pos = 0;
		}

		xzret = xz_dec_run(s, &b);
		if (xzret == XZ_OK || xzret == XZ_STREAM_END) {
			out_len += b.out_pos;
#ifdef XZ_DEC_FROM_FLASH
			//XZ_DEC_FILE_WRITE(0, out_buf, b.out_pos, status)
#else
			XZ_DEC_FILE_WRITE(out_buf, b.out_pos, bw);
#endif
		}
		if (xzret == XZ_OK) {
			b.out_pos = 0;
			continue;
		} else if (xzret == XZ_STREAM_END) {
			CMD_DBG("dec end, size %u --> %u\n", total_len, out_len);
			break;
		} else {
			CMD_ERR("xz_dec_run() fail %d\n", xzret);
			break;
		}
	}

	XZ_DEC_FILE_EXIT();

	xz_dec_end(s);

	free(in_buf);
	free(out_buf);

	return CMD_STATUS_OK;
}

static enum cmd_status cmd_xz_dec_rate_exec(char *cmd)
{
	struct xz_dec *s;
	struct xz_buf b;
	enum xz_ret xzret;

	uint32_t out_len = 0;

	uint8_t *out_buf = malloc(XZ_DEC_OUTBUF_SIZE);
	if (out_buf == NULL) {
		CMD_ERR("no mem\n");
		return CMD_STATUS_FAIL;
	}
	memset(out_buf, 0, XZ_DEC_OUTBUF_SIZE);

	s = xz_dec_init(XZ_DYNALLOC, XZ_DEC_DICT_MAX);
	if (!s) {
		CMD_ERR("no mem\n");
		free(out_buf);
		return CMD_STATUS_FAIL;
	}

	b.in = xz_array;
	b.in_pos = 0;
	b.in_size = sizeof(xz_array);
	b.out = out_buf;
	b.out_pos = 0;
	b.out_size = XZ_DEC_OUTBUF_SIZE;

	printf("b.in_size:%d\n", b.in_size);

	CMD_DBG("dec start..\n");
	uint32_t stop_time = 0;
	uint32_t start_time = OS_TicksToMSecs(OS_GetTicks());

	uint32_t i = 0;
	for (i = 0; i < XZ_RATE_COUNT; i++) {
		b.in_pos = 0;
		b.out_pos = 0;
		while (1) {
			xzret = xz_dec_run(s, &b);
			if (xzret == XZ_OK || xzret == XZ_STREAM_END) {
				out_len += b.out_pos;
				b.out_pos = 0;
			}
			if (xzret == XZ_OK) {
				continue;
			} else if (xzret == XZ_STREAM_END) {
				stop_time = OS_TicksToMSecs(OS_GetTicks());
				//CMD_DBG("dec end, rate: %fKB/S\n", (float)(out_len - b.in_size)/1024/((float)(stop_time-start_time)/1000));
				break;
			} else {
				CMD_ERR("xz_dec_run() fail %d\n", xzret);
				break;
			}
		}
		xz_dec_reset(s);
	}
	if (xzret == XZ_STREAM_END) {
		CMD_DBG("dec end, rate: %fKB/S\n", (float)(out_len - b.in_size * XZ_RATE_COUNT)/1024/((float)(stop_time-start_time)/1000));
	} else
		CMD_ERR("dec fail %d\n", xzret);

	xz_dec_end(s);

	free(out_buf);

	return CMD_STATUS_OK;
}

static const struct cmd_data g_xz_cmds[] = {
	{ "dec-file",        cmd_xz_dec_file_exec },
	{ "dec-rate",        cmd_xz_dec_rate_exec },
};

enum cmd_status cmd_xz_exec(char *cmd)
{
	return cmd_exec(cmd, g_xz_cmds, cmd_nitems(g_xz_cmds));
}
